<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of XinqingtempLogic
 *
 * @author Administrator
 */
class XinqingtempLogic extends BaseLogic{
    //put your code here
     private $xinqingtempTable;
    public function __construct() {
        parent::__construct();
        $this->xinqingtempTable=  $this->load("xinqing_temp");
        $this->table=  $this->xinqingtempTable;
    }
    /**
     * 判断温度权重信息是否已经更新过了，更新过了返回true,否则返回false
     * @param type $v 温度权重信息，其中必须包含user_id，xinqing_id，month，day,temp
     * @return boolean 更新过了返回true,否则返回false
     */
    public function isUpdate($v){
        $edit_time=date("Y-m-d");
        $c=  $this->xinqingtempTable->get_count("user_id={$v["user_id"]} and xinqing_id={$v["xinqing_id"]} and month={$v["month"]} and day={$v["day"]} and temp={$v["temp"]} and edit_time='{$edit_time}'");
        if($c>0){
            return TRUE;
        }
        return FALSE;
    }
    /**
     * 取出最有可能的温度权重值(温差在1度左右，如果没有取温差在3度左右)
     * @param type $user_id  用户id
     * @param type $temp     温度
     * @param type $month    月份
     * @param type $day      日期
     * @return int   返回心情id和温度权重值数组
     */
    public function getTempWeight($user_id,$temp,$month,$day){
        if($user_id>0){
            $t2=$temp+1;
            $t1=$temp-1;
            $list=  $this->xinqingtempTable->select("weight,xinqing_id", "user_id={$user_id} and temp>={$t1} and temp<={$t2} and month={$month} and day={$day}");
            $ret=array_rand($list,1);
            if($ret!==NULL){
                return $list[$ret];
            }
            
            $t2=$temp+3;
            $t1=$temp-3;
            $list2=  $this->xinqingtempTable->select("weight,xinqing_id", "user_id={$user_id} and temp>={$t1} and temp<={$t2} and month={$month} and day={$day}");
            $ret2=array_rand($list2,1);
            if($ret2!==NULL){
                return $list2[$ret2];
            }
            
            return FALSE;
        }
    }
    /**
     * 统计用户昨天的温度以及温度权重情况
     * @param type $user_id 用户id
     * @param type $month   月份
     * @param type $day     日期
     */
    public function updateXinQingTempInfo($user_id,$month,$day){
        $this->xinqingtempTable->delete("user_id={$user_id} and month={$month} and day={$day}");
        $sql="SELECT user_id,xinqing_id,`month`,`day`,temp,COUNT(temp)as count,COUNT(temp)/100-(sum(hum_temp_weight))/100 as weight from jp_user_xinqing where user_id={$user_id} and `month`={$month} and `day`={$day} and status=1 GROUP BY `month`,`day`,xinqing_id,temp";
        $list=$this->xinqingtempTable->execute($sql);
        if(!empty($list)){
            foreach ($list as $v){
                $v["edit_time"]=  date("Y-m-d");
                if($this->isUpdate($v)){
                    continue;
                }
                $yesterday=  $this->xinqingtempTable->get_one("*", "user_id={$v["user_id"]} and xinqing_id={$v["xinqing_id"]} and month={$v["month"]} and day={$v["day"]} and temp={$v["temp"]}");
                if($yesterday){
                    $this->save($v,$yesterday["id"]);
                }else{
                    $this->save($v);
                }
            }
            return TRUE;
        }else{
            return FALSE;
        }
    }
}
